package br.com.ravi.deposito.dao;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.type.StringType;

import br.com.ravi.deposito.utils.hibernate.HibernateUtil;
import br.com.ravi.deposito.vo.UserVO;
/**
 * 
 * @author Rafael Ingaramo
 *
 */
public class LoginDAO {
	public UserVO login(String username, String password) {
		Session session = HibernateUtil.getCurrentSession();
		
		StringBuilder hql = new StringBuilder();
		
		hql.append(" SELECT caduser FROM br.com.ravi.deposito.vo.UserVO AS caduser");
		hql.append("  WHERE caduser.descricaoUsuario = :dcrUsuario ");
		hql.append("    AND caduser.senha = :dcrSenha ");
		
		Transaction tx = session.beginTransaction();
		Query query = session.createQuery(hql.toString());
		query.setParameter("dcrUsuario", username, StringType.INSTANCE);
		query.setParameter("dcrSenha", password, StringType.INSTANCE);
		query.setMaxResults(1);
		UserVO userVO = (UserVO) query.uniqueResult();
		tx.commit();
		
		return userVO;
	}
}